package com.example.buxiaorui.studentsystem;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
    private EditText et_username;
    private EditText et_passwod;
    private CheckBox cb;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_username=(EditText)findViewById(R.id.et_login_user);
        et_passwod=(EditText)findViewById(R.id.et_login_password);
        cb=(CheckBox)findViewById(R.id.cb);
        sp=getSharedPreferences("config",MODE_PRIVATE);
        String username=sp.getString("username","");
        String password=sp.getString("password","");
        et_username.setText(username);
        et_passwod.setText(password);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }
    //跳转到注册页面
    public void to_regist(View view){
        Intent intent=new Intent(this,RegistActivity.class);
        startActivity(intent);
        overridePendingTransition(0, 0);

    }
    //验证密码是否正确， 跳转到主页面
    public void login(View view){
        SqlDao sqlDao=new SqlDao(this);
        String name=et_username.getText().toString().trim();
        boolean res=sqlDao.checkdata(name);
        String password=et_passwod.getText().toString().trim();


        if (res==false){
            DialogManager dialogManager=new DialogManager(this);
            dialogManager.showErrorUser();
            return;
        }
        String sql_password=sqlDao.getPassword(name);
        if (TextUtils.equals(sql_password,password)){
            Intent intent=new Intent(this,StudentsListActivity.class);
            startActivity(intent);
            finish();
            record_status();
        }else {
            DialogManager dialogManager=new DialogManager(this);
            dialogManager.showErrorLogin();
        }
        if (cb.isChecked()){
            sp=getSharedPreferences("config",MODE_PRIVATE);
            SharedPreferences.Editor editor=sp.edit();
            editor.putString("username",et_username.getText().toString().trim());
            editor.putString("password",et_passwod.getText().toString().trim());
            editor.apply();
        }
        record_status();
    }
    //记录登录状态，写入sharedpreference
    public void record_status(){
        sp=getSharedPreferences("login_status",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("status","true");
        editor.apply();
    }

    //生成一个基础数据
    public void create_new(View view){
        SqlDao sqlDao=new SqlDao(this);
        sqlDao.addManager("test","1234",1234);
        DialogManager dialogManager=new DialogManager(this);
        dialogManager.showAddsuccess();
    }

}
